package com.shanzmoo.db.product.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.db.product.entity.ProductAttributeEntity;
import com.shanzmoo.db.product.entity.vo.*;
import com.shanzmoo.db.product.mapper.ProductAttributeMapper;
import com.shanzmoo.db.product.service.IProductAttributeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * <p>
 * 商品属性 服务实现类
 * </p>
 *
 * @author Xueqz
 * @since 2020-12-07
 */
@Service
public class ProductAttributeServiceImpl extends ServiceImpl<ProductAttributeMapper, ProductAttributeEntity> implements IProductAttributeService {

    @Override
    public SResponse<PageRespBase<ProductAttributeEntity>> getAttributeList(AttributeQueryVO attributeQueryVO) {
        // 开启分页
        Page page = PageHelper.startPage(attributeQueryVO.getPageNum(), attributeQueryVO.getPageSize());
        //获取信息 -- start
        List<ProductAttributeEntity> productAttributeEntityList = getAttributeListMethod(attributeQueryVO);
        //获取信息 -- end
        PageRespBase<ProductAttributeEntity> re = PageRespBase.create(page, productAttributeEntityList);
        PageHelper.clearPage();
        return SResponse.ok(re);
    }

    public List<ProductAttributeEntity> getAttributeListMethod(AttributeQueryVO attributeQueryVO){
        return baseMapper.selectList(new QueryWrapper<ProductAttributeEntity>()
                .eq("deleted", false));
    }

    @Override
    public PushAttributeVO getPushAttributeVoList() {
        List<ProductAttributeEntity> productAttributeEntityList = baseMapper.selectList(new QueryWrapper<ProductAttributeEntity>()
                .eq("deleted", false));
        PushAttributeVO pushAttributeVO = handlerAtributeToAttributeVO(productAttributeEntityList);
        return pushAttributeVO;
    }


    private PushAttributeVO handlerAtributeToAttributeVO(List<ProductAttributeEntity> productAttributeEntityList) {
        PushAttributeVO pushAttributeVO = new PushAttributeVO();
        if(productAttributeEntityList != null && productAttributeEntityList.size() > 0){
            List<AttributeVO> attributeVOList = new ArrayList();
            for (ProductAttributeEntity productAttributeEntity : productAttributeEntityList) {
                AttributeVO attributeVO = new AttributeVO();
                attributeVO.setAttrId(productAttributeEntity.getId());
                attributeVO.setAttrName(productAttributeEntity.getName());
                String value = productAttributeEntity.getValue();
                if(StrUtil.isNotBlank(productAttributeEntity.getValue())){
                    String[] valueArr = value.split("\\|");
                    attributeVO.setAttributeValues(new ArrayList<>(Arrays.asList(valueArr)));
                }else{
                    attributeVO.setAttributeValues(new ArrayList<>());
                }
                attributeVOList.add(attributeVO);
            }
            pushAttributeVO.setAttributeVOList(attributeVOList);
        }
        pushAttributeVO.setCheckAttributeVOList(new ArrayList<>());
        return pushAttributeVO;
    }


}

